<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>部门管理</el-breadcrumb-item>
      <el-breadcrumb-item>部门列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--查询部门-->
    <el-row :gutter="20">
      <el-col :span="8">
        <el-input placeholder="请输入部门名称" clearable v-model="dname" @clear="getDeptList">
          <el-button slot="append" icon="el-icon-search" @click="getDeptList"></el-button>
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="primary" @click="addDialogVisible=true">添加部门</el-button>
      </el-col>
    </el-row>
    <!--添加部门-->
    <el-dialog title="添加部门" :visible.sync="addDialogVisible" width="50%" :close-on-click-modal="false"
               @close="addDeptFormClose">
      <el-form
          :model="addDeptForm"
          :rules="addDeptFormRules"
          ref="addDeptFormRef"
          label-width="100px"
          class="demo-ruleForm"
      >
        <el-form-item label="部门编码" prop="did">
          <el-input v-model="addDeptForm.did"></el-input>
        </el-form-item>
        <el-form-item label="部门名称" prop="dname">
          <el-input v-model="addDeptForm.dname"></el-input>
        </el-form-item>

        <el-form-item label="上级部门">
          <el-select v-model="addDeptForm.parentId" placeholder="上级部门" prop="parentId">
            <el-option :label="item.dname" :value="item.id" v-for="item in deptList1" :key="item.id"></el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="部门类型" prop="dtype">
          <el-input v-model="addDeptForm.dtype"></el-input>
        </el-form-item>
        <el-form-item label="部门编制" prop="bsize">
          <el-input v-model="addDeptForm.bsize"></el-input>
        </el-form-item>
        <el-form-item label="实际人数" prop="dsize">
          <el-input v-model="addDeptForm.dsize"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="description">
          <el-input v-model="addDeptForm.description"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addDept">确 定</el-button>
        <el-button @click="cancelAddDept">取 消</el-button>
      </span>
    </el-dialog>

    <el-table
        :data="deptList"
        stripe
        style="width: 100%">
      <el-table-column
          prop="id"
          label="编号"
          width="180">
      </el-table-column>
      <el-table-column
          prop="did"
          label="部门编码"
          width="180">
      </el-table-column>
      <el-table-column
          prop="dname"
          label="部门名称"
          width="180">
      </el-table-column>
      <el-table-column
          prop="parentId"
          label="上级部门">
      </el-table-column>

      <el-table-column
          prop="dtype"
          label="部门类型">
        <template slot-scope="dt">
          <span v-text="dt.row.dtype == 0 ? '总部': '部门'"/>
        </template>
      </el-table-column>

      <el-table-column
          prop="bsize"
          label="部门编制">
      </el-table-column>
      <el-table-column
          prop="dsize"
          label="实际人数">
      </el-table-column>
      <el-table-column
          prop="comeTime"
          label="生效时间">
      </el-table-column>
      <el-table-column
          prop="description"
          label="备注">
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="创建时间">
      </el-table-column>
      <el-table-column
          prop="updateTime"
          label="更新时间">
      </el-table-column>

      <el-table-column fixed="right" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="warning" size="mini" @click="showEditDialog(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="removeDeptById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
<!--    分页-->
    <div class="block">
      <span class="demonstration"></span>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[3, 6, 9, 12]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

<!--    修改部门-->
    <el-dialog
        title="修改部门"
        :visible.sync="editDialogVisible"
        width="50%"
        @close="editDeptFormClose"
        :close-on-click-modal="false"
    >
      <el-form
          :model="editDeptForm"
          :rules="editDeptFormRules"
          ref="editDeptFormRef"
          label-width="100px"
          class="demo-ruleForm"
      >
        <el-form-item label="部门编码" prop="did">
          <el-input v-model="editDeptForm.did" disabled></el-input>
        </el-form-item>
        <el-form-item label="部门名称" prop="dName">
          <el-input v-model="editDeptForm.dname"></el-input>
        </el-form-item>

        <el-form-item label="上级部门">
          <el-select v-model="editDeptForm.parentId" placeholder="上级部门" prop="parentId">
            <el-option :label="item.dname" :value="item.id" v-for="item in deptList1" :key="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="部门类型" prop="dtype">
          <el-input v-model="editDeptForm.dtype"></el-input>
        </el-form-item>
        <el-form-item label="编制人数" prop="bsize">
          <el-input v-model="editDeptForm.bsize"></el-input>
        </el-form-item>
        <el-form-item label="实际人数" prop="dsize">
          <el-input v-model="editDeptForm.dsize"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="description">
          <el-input v-model="editDeptForm.description"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editDept">确 定</el-button>
        <el-button @click="editDialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>

  </div>

</template>

<script>
export default {
  data() {
    return {
      deptList: [],
      deptList1: [],
      pageSize: 3,
      pageNum: 1,
      total: 2,
      dname: '',
      addDialogVisible: false,
      editDialogVisible: false,
      addDeptForm: {
        did: "",
        dname: "",
        parentId: "",
        dtype: "",
        bsize: "",
        dsize: "",
        cometime: "",
        description: "",
        createTime: "",
        updateTime: ""
      },
      //添加部门输入规则
      addDeptFormRules: {

      },
      editDeptForm: {
        did: "",
        dname: "",
        parentId: "",
        dtype: "",
        bsize: "",
        dsize: "",
        description: "",
      },
      editDeptFormRules: {

      },

    }
  },
  methods: {
    // 查询所有部门
    getDeptList() {
      let url = this.$HRMSUrl + "/dept/listAllDept"
      this.$axios.get(url, {
        params: {
          name: this.dname,
          currentPage: this.pageNum,
          pageSize: this.pageSize,
          total: this.total
        }
      })
          .then(res => {
            if (res.data.code == 200) {
              this.deptList = res.data.data.list;
              this.pageNum = res.data.data.pageNum;
              this.pageSize = res.data.data.pageSize;
              this.total = res.data.data.total;

            } else {
              this.$message({
                type: "error",
                message: res.data.msg,
                duration: 1000
              })
            }
          })
    },

    //查询所有部门列表
    initDept() {
      let url = this.$HRMSUrl + "/dept/initDept"
      this.$axios.get(url)
          .then(res => {
            if (res.data.code == 200) {
              this.deptList1 = res.data.data;
            } else {
              this.$message({
                type: "error",
                message: res.data.msg,
                duration: 1000
              })
            }
          })

    },
    //添加部门
    addDept() {
      this.$refs.addDeptFormRef.validate(valid => {
        if (valid) {
          let url = this.$HRMSUrl + "/dept/addDept";
          this.$axios.post(url, this.addDeptForm)
              .then(res => {
                if (res.data.code === 200) {
                  this.addDialogVisible = false;
                  this.initDept();
                  this.getDeptList();
                } else {
                  this.$message({
                    type: "error",
                    message: res.data.msg,
                    duration: 1000
                  })
                }
              })
        } else {
          return false;
        }
      })
    },

    //取消添加
    cancelAddDept() {
      this.addDialogVisible = false;
      this.addDeptForm = {};
    },

    //关闭添加对话框
    addDeptFormClose() {
      this.$refs.addDeptFormRef.resetFields();

    },

    //显示修改框
    showEditDialog(row){
      this.editDialogVisible = true;
      this.editDeptForm = row;
    },

    //关闭编辑对话框
    editDeptFormClose(){
      this.$refs.editDeptFormRef.resetFields();
    },

    //修改部门
    editDept(){
      let url = this.$HRMSUrl + "dept/editDept";
      this.$axios.put(url,this.editDeptForm)
      .then(res => {
        if (res.data.code === 200) {
          this.editDialogVisible = false;
          this.getDeptList();
        }else {
          this.$message({
            type: "error",
            message: res.data.msg,
            duration: 1000
          })
        }
      })
    },

    //删除部门
    removeDeptById(id){
      let url = this.$HRMSUrl + "/dept/delete/" + id;
      this.$confirm('此数据将将永久删除该数据，是否继续？','提示',{
        confirmButtonText:'确定',
        cancelButtonText:'取消',
        type:'warning'
      }).then(() => {
        this.$axios.put(url).then(res =>{
          if (res.data.code === 200){
            this.getDeptList();
          }else {
            this.$message({
              type:"error",
              message:res.data.msg,
              duration:1000
            })
          }
        })
      }).catch(() => {
        this.$message({
          type:"info",
          message:"已取消删除"
        })
      })


    },


    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.getDeptList();
    },

    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.getDeptList()
    },

    loadMenus() {
      this.getDeptList();
      this.initDept();
    }
  },

  created() {
    this.loadMenus();
  }
}
</script>